//
//  IFIndexSlidesCell.m
//  chanlin
//
//  Created by Ryan on 14-11-9.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "IFIndexSlidesCell.h"
#import "UIView+Utils.h"
#import "UIImageView+WebCache.h"
#import "EasyTableView.h"
#import "CLUrl.h"
#import "SportTopicSubSliderCell.h"

@interface IFIndexSlidesCell ()
@property (nonatomic,strong)NSMutableArray *images;
@property (nonatomic,strong) UIImageView *commentIcon;
@property (nonatomic,strong) UILabel *commentLable;
@property (nonatomic,strong)EasyTableView *tableView;
@property (nonatomic,strong) CLList *list;
@property (nonatomic,strong) CLCollect *colect;
@property (nonatomic,strong) CLTopic *topic;

@end

@implementation IFIndexSlidesCell

- (void)uiInit{
    [super uiInit];
    self.cellSeperatorImgView=[CALayer layer];
    self.cellSeperatorImgView.frame=CGRectMake(0, 0.f, GlobleWidth, 0.5f);
    self.cellSeperatorImgView.backgroundColor=RGB(0xe0, 0xe0, 0xde).CGColor;
    [self.baseView.layer addSublayer:self.cellSeperatorImgView];
    
    self.titleLabel = [[IFIndexTitleView alloc] initWithFrame:CGRectMake(cellEdgeCap, cellEdgeCap, cellLongTitleWidth, 20.f)];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.textColor=RGB(0x4a, 0x4d, 0x53);
    self.titleLabel.font = [UIFont systemFontOfSize:15.f];
    self.titleLabel.height=self.titleLabel.font.lineHeight+10;
   // self.titleLabel.numberOfLines = 1;
    [self.baseView  addSubview:self.titleLabel];
    
    
//    // 背景容器
//    UIView *baseView=[[UIView alloc] init];
//    baseView.frame=CGRectMake(cellEdgeCap, self.titleLabel.bottom+cellEdgeCap-3, GlobleWidth-2*cellEdgeCap, 67.0f);
//    baseView.backgroundColor=[UIColor clearColor];
//    _images = [NSMutableArray array];
//    // --三张图片
//    for (int i=0; i<3; i++) {
//        UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"index_slide_default.png"]];
//        image.frame=CGRectMake((i*4.0f)+(i*97.0f), 0.0f, 97.0f, 67.0f);
//        
//        [self.images sgrAddObject:image];
//        [baseView addSubview:image];
//    }
//    [self.baseView  addSubview:baseView];
    
    
    EasyTableView *tableView = [[EasyTableView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom+cellEdgeCap-3, GlobleWidth, 67)];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.pageEnable = NO;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.tableStyle = UITableViewStylePlain;
    [self addSubview:tableView];
    self.tableView = tableView;
    [tableView setupSubViews];
    tableView.userInteractionEnabled = YES;
    
    [self.baseView  addSubview:tableView];
   // tableView.tableView.contentInset=UIEdgeInsetsMake(11.f, 0, 11, 0);
    
    _commentIcon = [[UIImageView alloc] initWithFrame:CGRectMake(GlobleWidth-47, tableView.bottom+cellEdgeCap-5, 13.f, 11.f)];
     _commentIcon.image=[UIImage imageNamed:@"index_biaozhi.png"];
    [self addSubview:_commentIcon];
    
    _commentLable = [[UILabel alloc] initWithFrame:CGRectMake(_commentIcon.right+4.f, tableView.bottom+cellEdgeCap-5, 50.f, 12.f)];
    _commentLable.textAlignment = NSTextAlignmentLeft;
    _commentLable.textColor=RGB(0x5b, 0x85, 0x9e);
    [self.baseView  addSubview:_commentLable];

}

- (void)loadData:(CLList *)newsList{
    self.list=newsList;
    self.titleLabel.text = newsList.title;
    
        [self.tableView reloadData];
        [self.tableView.tableView setContentOffset:CGPointMake(0, -11) animated:NO];
    _commentIcon.hidden=YES;
    _commentLable.hidden=YES;
    if(_isStrNotNull(newsList.sub)){
        _commentIcon.hidden=NO;
        _commentLable.hidden=NO;
        _commentLable.text=newsList.sub;
    }
  
//    for(int i=0;i<[self.images count];i++){
//        NSString *str=nil;
//        if(i==0) str=newsList.slider1;
//        else if(i==1) str=newsList.slider2;
//        else if(i==2) str=newsList.slider3;
//        if(_isStrNULL(str)) continue;
//        UIImageView *image=[self.images sgrGetType:[UIImageView class] forIndex:i];
//        [image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"index_slide_default.png"]];
//       
//    }

}

- (void)loadTopicData:(CLTopic *)newsList{
    self.topic=newsList;
    self.titleLabel.text = newsList.title;
    
        [self.tableView reloadData];
        [self.tableView.tableView setContentOffset:CGPointMake(0, -11) animated:NO];
    _commentIcon.hidden=YES;
    _commentLable.hidden=YES;
    if(_isStrNotNull(newsList.sub)){
        _commentIcon.hidden=NO;
        _commentLable.hidden=NO;
        _commentLable.text=newsList.sub;
    }
    
//    for(int i=0;i<[self.images count];i++){
//        NSString *str=nil;
//        if(i==0) str=newsList.slider1;
//        else if(i==1) str=newsList.slider2;
//        else if(i==2) str=newsList.slider3;
//        if(_isStrNULL(str)) continue;
//        UIImageView *image=[self.images sgrGetType:[UIImageView class] forIndex:i];
//        [image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"index_slide_default.png"]];
//        
//    }

}

- (void)setIsRead:(BOOL)isRead{
    self.titleLabel.textColor=isRead?[UIColor grayColor]:RGB(0x4a, 0x4d, 0x53);
}

- (void)loadCollect:(CLCollect *)collect{
    self.colect=collect;
    self.titleLabel.text = collect.title;
    
        [self.tableView reloadData];
        [self.tableView.tableView setContentOffset:CGPointMake(0, -11) animated:NO];
    _commentIcon.hidden=YES;
    _commentLable.hidden=YES;
    if(_isStrNotNull(collect.sub)){
        _commentIcon.hidden=NO;
        _commentLable.hidden=NO;
        _commentLable.text=collect.sub;
    }
    
//    for(int i=0;i<[self.images count];i++){
//        NSString *str=nil;
//        if(i==0) str=collect.slider1;
//        else if(i==1) str=collect.slider2;
//        else if(i==2) str=collect.slider3;
//        if(_isStrNULL(str)) continue;
//        UIImageView *image=[self.images sgrGetType:[UIImageView class] forIndex:i];
//        [image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"index_slide_default.png"]];
//        
//    }
}


 #pragma mark - Easy tableview delegate & datasource methods
 
 - (NSInteger)easyTableView:(EasyTableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     
     if(self.colect){
         return self.colect.subCurl.count;
     }else if(self.topic){
         return self.topic.subtopicUrl.count;
     }else if(self.list){
         return self.list.subUrls.count;
     }
     
     return 0;
 }
 
 - (NSInteger)easyTableViewNumberOfSectionsInTableView:(EasyTableView *)tableView
 {
 return 1;
 }
 
 - (CGFloat)easyTableView:(EasyTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 //    if(indexPath.row != [self.indexList.subIndexs count] -1)
 //    {
 //        return 124;
 //    }
 return 100.f;
 }
 
 - (SportTopicSubSliderCell *)easyTableView:(EasyTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSString *identfier = @"Slide3Cell";
 SportTopicSubSliderCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
 if(cell == nil)
 {
 cell = [[SportTopicSubSliderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier];
 cell.backgroundColor = [UIColor clearColor];
 cell.contentView.backgroundColor = [UIColor clearColor];
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 }
// IFSubIndexList *subIndexlist = [_subNewsListArray sgrGetType:[IFSubIndexList class] forIndex:indexPath.row];
// if(subIndexlist && [subIndexlist isKindOfClass:[IFSubIndexList class]]){
// cell.sourceData =[NSDictionary dictionaryWithObject:subIndexlist.imgUrl forKey:@"thumbnail"];
// }
     if(self.colect){
         CLUrl *cu=[self.colect.subCurl objectAtIndex:indexPath.row];
         cell.url=cu.url;
     }else if(self.topic){
         CLUrl *cu=[self.topic.subtopicUrl objectAtIndex:indexPath.row];
         cell.url=cu.url;
        // return self.topic.subtopicUrl.count;
     }else if(self.list){
         CLUrl *cu=[self.list.subUrls objectAtIndex:indexPath.row];
         cell.url=cu.url;
        // return self.list.subUrls.count;
     }
     
 [cell setupSubViewWithWidth:97.f height:67.f];
 return cell;
 }
 
 - (void)easyTableView:(EasyTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if([self.delegate respondsToSelector:@selector(didSelect:)]){
         [self.delegate performSelector:@selector(didSelect:) withObject:self.indexPath];
     }
 //    if(_blkTapResponse)
 //    {
 //        _blkTapResponse(self.indexList,0,indexPath);
 //    }
 }

//- (CGFloat)easyTableView:(EasyTableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 11.f;
//}
//- (CGFloat)easyTableView:(EasyTableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 11.f;
//}

@end
